//
//  ViewController.m
//  调度组
//
//  Created by hu yr on 2017/2/7.
//  Copyright © 2017年 terry. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self group1];
}


- (void)group2{
    
    //1.创建调度组
    dispatch_group_t group = dispatch_group_create();
    
    //2.队列
    dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    
    //3. 调度组
    /*
     1.调度组没有任务,直接执行 notify
     2.入组多于出组,notify永远不会执行,因为组永远不会为空
     3.出组多于入组,会崩溃
     
     入组,出组需要成对的出现
     */
    //入组
    dispatch_group_enter(group);
    dispatch_async(q, ^{
        NSLog(@"download A %@",[NSThread currentThread]);
        
        //出租
        dispatch_group_leave(group);
    });
    
    //入组
    dispatch_group_enter(group);
    dispatch_async(q, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"download B %@",[NSThread currentThread]);
        
        //出租
        dispatch_group_leave(group);
    });
    
    
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"come here %@",[NSThread currentThread]);
        
    });
}

/**
 调度组最重要的目的 是 监听 一组任务完成
 */
- (void)group1{
    
    //1.创建调度组
    dispatch_group_t group = dispatch_group_create();
    
    //2.队列
    dispatch_queue_t q = dispatch_get_global_queue(0, 0);
    
    //3.调度组监听队列调度任务
    dispatch_group_async(group, q, ^{
        
        NSLog(@"download A %@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, q, ^{
        NSLog(@"download B %@",[NSThread currentThread]);
    });
    
    
    
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    NSLog(@"come here %@",[NSThread currentThread]);
        
    });
    
    
    
}


@end
